import { defineStore } from 'pinia'
import { randomString } from '../utils/public'
import { LoginData } from '../types/loginInterface'
import request from '../http/index'
import { message } from 'ant-design-vue'
import router from '../router/index'

interface NoteItemInfo {
  id: number
  noteType: string
  noteMessage: string
  state: boolean
}

interface NoteInfo {
  unreadCount: number
  noteList: Array<NoteItemInfo>
}

export const useInfoStore = defineStore('useInfoStore', {
  state: () => ({
    userName: '',
    noteInfo: {
      unreadCount: 0,
      noteList: [],
    } as NoteInfo,
    avatarUrl: ''
  }),
  actions: {
    setUserName(value: string) {
      this.userName = value
    },
    /**
     * 
     * @param avatarUrl 
     * 修改用户头像
     */
    updateAvatarUrl(avatarUrl: string) {
      this.avatarUrl = avatarUrl
    },
    /**
     * 获取Note通知列表
     */
    getNoteInfo() {
      let count = Math.floor(Math.random() * 10)
      this.noteInfo.unreadCount = count
      let noteList = []
      for (let i = 0; i < count; i++) {
        let item = {
          id: i + 1,
          noteType: randomString(5),
          noteMessage: randomString(25),
          state: false
        }
        noteList.push(item)
      }
      this.noteInfo.noteList = noteList
    },
    /**
     * 
     * @param noteId 
     * 修改通知状态及未读数据
     */
    updatedNoteState(noteId: number) {
      for (let item of this.noteInfo.noteList) {
        if (item.id == noteId) {
          item.state = true
        }
      }
      this.noteInfo.unreadCount = this.noteInfo.unreadCount - 1
    }
  },
})